package com.hongkuncheng.cms.controller.interfaces;

import java.io.IOException;
import java.util.Date;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hongkuncheng.cms.helper.SecurityHelper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Controller;
import com.hongkuncheng.cms.controller.BaseController;
import com.hongkuncheng.cms.model.Admin;
import com.hongkuncheng.cms.model.ArticlePush;
import com.hongkuncheng.cms.model.Organization;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 单位接口
 *
 * @author 洪坤成
 */
@Controller
@Transactional
@RequestMapping("interfaces/organization")
@ResponseBody
public class InorganizationController extends BaseController {

    // 被脱离
    @RequestMapping("deattach")
    public void deattach() {
        factorySession.createSQLQuery("DELETE FROM " + Organization.class.getSimpleName()
                .toUpperCase()).executeUpdate();
    }

    // 更新单位
    @RequestMapping("update")
    public void update() {
        // 清空所有单位数据
        try {
            factorySession.createSQLQuery("DELETE FROM " + Organization.class.getSimpleName()
                    .toUpperCase()).executeUpdate();
            String jsonstr = request.getParameter("jsonstr");
            JSONArray array = JSON.parseArray(jsonstr);
            for (int i = 0; i < array.size(); i++) {
                JSONObject object = array.getJSONObject(i);
                Organization organization = new Organization();
                organization.setName(SecurityHelper.aesDecrypt(object.getString("name")));
                organization.setProject(SecurityHelper.aesDecrypt(object.getString("project")));
                organization.setOutterhost(SecurityHelper.aesDecrypt(object.getString("outterhost")));
                organization.setInnerhost(SecurityHelper.aesDecrypt(object.getString("innerhost")));
                organization.setAttach(true);
                organization.setSort(i + 1);
                factorySession.save(organization);
            }
            print(1);
        } catch (Exception e) {
            print(0);
        }
    }

    // 重置管理员密码
    @RequestMapping("resetpassword")
    public void resetpassword() {
        Admin admin = factorySession.load(Admin.class, 1);
        admin.setPassword(SecurityHelper.md5Encrypt(admin.getAccount() + "_" + "123456"));
    }

    // 接收报送
    @RequestMapping("receivepush")
    public void receivepush() {
        try {
            String json = request.getParameter("articlejsondata").replace("'", "\"");
            JSONArray array = JSON.parseArray(json);
            for (int i = 0; i < array.size(); i++) {
                JSONObject obj = array.getJSONObject(i);
                ArticlePush article = new ArticlePush();
                article.setTitle(SecurityHelper.aesDecrypt(obj.getString("title")));
                article.setSource(SecurityHelper.aesDecrypt(obj.getString("source")));
                article.setAuthor(SecurityHelper.aesDecrypt(obj.getString("author")));
                article.setDigest(SecurityHelper.aesDecrypt(obj.getString("digest")));
                article.setExturl(SecurityHelper.aesDecrypt(obj.getString("exturl")));
                article.setContents(SecurityHelper.aesDecrypt(obj.getString("contents")));
                if (obj.keySet().contains("image")) article.setImage(SecurityHelper.aesDecrypt(obj.getString("image")));
                article.setPusher(SecurityHelper.aesDecrypt(obj.getString("pusher")));
                article.setPushtime(new Date());
                factorySession.save(article);
            }
            print(1);
        } catch (Exception e) {
            e.printStackTrace();
            print(0);
        }
    }

}
